package com.cw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.common.R;
import com.cw.config.JwtConfig;
import com.cw.entity.ShoppingCart;
import com.cw.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加菜品到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        //1.获取数据
        log.info("添加到购物车的菜品为：{}",shoppingCart);

        //2.确认登录信息
        Long userId = JwtConfig.getCurrentId(request);
        //3.创建查询条件
        //4.菜品跟套餐为同一个表，需要区分传入id
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        wrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        //5.查询数据库
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        //6.判断如果数据库没有就添加一个
        if (cart == null){
            ShoppingCart shoppingCart1 = new ShoppingCart();
            //添加基本数据
            BeanUtils.copyProperties(shoppingCart,shoppingCart1);
            //健壮性维护
            shoppingCart1.setUserId(userId);
//            shoppingCart1.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart1);
            return R.success(shoppingCart1);
        }


//        cart.setUserId(userId);
//        cart.setCreateTime(LocalDateTime.now());
        cart.setNumber(cart.getNumber()+1);
        shoppingCartService.updateById(cart);

        //返回参数
        return R.success(shoppingCart);
    }

    /**
     * 购物车显示
     * @param
     * @return
     */
    @GetMapping("/list")
    public  R list(HttpServletRequest request){
        Long userId =  JwtConfig.getCurrentId(request);

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId !=null,ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        return R.success(list);
    }

    /**
     * 购物车删除菜品
     */
    @PostMapping("/sub")
    public R delete(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        //获取参数信息

        //获取session
        Long userId =  JwtConfig.getCurrentId(request);

        //id存入ShoppingCart
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getUserId()!=null,ShoppingCart::getUserId,userId);
        wrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        //获取数据库菜品对象
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        one.setUserId(userId);
        Integer number = one.getNumber();
            if (number > 1) {
                one.setNumber(number - 1);
                shoppingCartService.updateById(one);
                R.success("数量减一");
            }
            if (number==1){
                shoppingCartService.removeById(one.getId());
            }
        return R.success("修改完成");
    }

    /**
     * 清空购物车
     */

    @DeleteMapping("/clean")
    public R clean(HttpServletRequest request){

        Long userId =  JwtConfig.getCurrentId(request);
        //构建条件
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        boolean b = shoppingCartService.remove(wrapper);

        return b?R.success("购物车已清空"):R.error("购物车清空失败");
    }
}
